//jquery将日期转换成指定格式的字符串
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


/**  
* 将秒数换成时分秒格式   
*/     
function formatSeconds(value) {  
    var theTime = parseInt(value);// 秒  
    var theTime1 = 0;// 分  
    var theTime2 = 0;// 小时  
    if(theTime > 60) {  
        theTime1 = parseInt(theTime/60);  
        theTime = parseInt(theTime%60);  
            if(theTime1 > 60) {  
            theTime2 = parseInt(theTime1/60);  
            theTime1 = parseInt(theTime1%60);  
            }  
    }  
        var result = ""+parseInt(theTime)+"秒";  
        if(theTime1 > 0) {  
        result = ""+parseInt(theTime1)+"分"+result;  
        }  
        if(theTime2 > 0) {  
        result = ""+parseInt(theTime2)+"时"+result;  
        }  
    return result;  
}


function formatSeconds2(value) {  
    var theTime = parseInt(value);// 秒  
    var theTime1 = 0;// 分    
    if(theTime > 60) {  
        theTime1 = parseInt(theTime/60);  
        theTime = parseInt(theTime%60);  
    }  
    var result = (theTime + "").padStart(2,0);  
    if(theTime1 > 0) {  
    	result = ("" + theTime1).padStart(2,0) +":"+ result;  
    } 
    return result;  
}  


/* 播放历史 */
function his_save(code, vid, ep, name, cover, progress){
	
	var now = new Date();
	var date = now.Format("yyyy-MM-dd");
	var time = now.Format("HH:mm:ss");	
	
	var his = new Object();
	his.code = code;
	his.vid = vid; 
	his.name = name;
	his.ep = ep;
	his.cover = cover;
	his.progress = formatSeconds(progress);
	his.progressSeconds = progress;
	// 日期
	his.date = date;
	// 时间
	his.time = time;
	// 时间戳
	his.timestamp = now.getTime();
	
	var his_map = $.zui.store.get('play_history');
	if(his_map == null){
		his_map = new Map();
	}
	var key = date + '-' + code + '-' + vid;
	his.key = key;
	
	his_map[key] = his;
	$.zui.store.set('play_history', his_map);
	
	console.log(name, '播放至', progress, '保存中...');
}

/* 获取当前播放历史 */
function his_get(){
	var his_map = $.zui.store.get('play_history');
	if(his_map == null){
		his_map = new Map();
	}
	return his_map;
}

/** 删除播放历史 */
function his_del(key){
	var his_map = $.zui.store.get('play_history');
	if(his_map == null){
		return;
	}
	his_map = new Map(Object.entries(his_map));
	his_map.delete(key);
	
	result_map = new Map();
	var idx = 0;
	his_map.forEach(function(value, ikey, map){	
		if(idx >= 50){
			return;
		}
		if(value != null){
			result_map[ikey] = value;
		}
		idx += 1;
	});
	
	$.zui.store.set('play_history', result_map);
	
	location.reload();
}


